package com.hnevc.musicplayer;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;

import android.content.ComponentName;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.Handler;
import android.os.IBinder;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.SeekBar;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {

    private static SeekBar musicSeekBar;
    private static TextView progress_tv;
    private static TextView total_tv;
    private Button play_btn;
    private Button pause_btn;
    private Button continue_btn;
    private Button exit_btn;
    private MusicService.MusicControl musicControl;
    private Boolean isUnBind = false;
    private MyServiceConn conn;
    private Intent intent;
    private static final String TAG = "MainActivity";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();
        initData();
        initEvent();
    }


    private void initView() {
        musicSeekBar = findViewById(R.id.music_seekbar);
        progress_tv = findViewById(R.id.progress_tv);
        total_tv = findViewById(R.id.total_tv);
        play_btn = findViewById(R.id.play_btn);
        pause_btn = findViewById(R.id.pause_btn);
        continue_btn = findViewById(R.id.continue_btn);
        exit_btn = findViewById(R.id.exit_btn);
    }

    private void initData() {
        intent = new Intent(MainActivity.this,MusicService.class);
        //创建连接服务
        conn = new MyServiceConn();
        bindService(intent, conn,BIND_AUTO_CREATE); //第一个告诉绑定服务工具 需要绑定的服务 第二个 创建连接服务 第三个 绑定后自动创建
    }

    private void initEvent() {
        play_btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                musicControl.play();
            }
        });
        //暂停
        pause_btn.setOnClickListener(new View.OnClickListener(){
            public void onClick(View v){
                musicControl.pause();
            }
        });
        //继续
        continue_btn.setOnClickListener(new View.OnClickListener(){
            public void onClick(View v){
                musicControl.continuePlay();
            }
        });
        //退出
        exit_btn.setOnClickListener(new View.OnClickListener(){
            public void onClick(View v){
                unBind(isUnBind);//解绑服务绑定
                isUnBind = true;
                finish();


            }
        });
        musicSeekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
            //进度条发生改变时
            @Override
            public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {

            }
            //滑动条滑动时
            @Override
            public void onStartTrackingTouch(SeekBar seekBar) {

            }
            //滑动条停止滑动时
            @Override
            public void onStopTrackingTouch(SeekBar seekBar) {
                int progress = seekBar.getProgress();
                musicControl.seekTo(progress);
            }
        });

    }
    public static Handler handler = new Handler(){

        @Override
        public void handleMessage(@NonNull Message msg) {
            Bundle bundle = msg.getData(); //获取服务传递过来的数据（子线程传递的数据）
            int duration = bundle.getInt("duration"); //总时长
            int currentPosition = bundle.getInt("currentPosition");//播放进度
            musicSeekBar.setMax(duration); //设置当前进度条的总长度
            musicSeekBar.setProgress(currentPosition); //当前播放进度

            int minute = duration / 1000 / 60;
            int second = duration / 1000 % 60;
            String strMinute = null;
            String strSecond = null;
            if (minute < 10){
                strMinute = "0" + minute;
            }else {
                strMinute = minute + "";
            }
            if (second < 10){
                strSecond = "0" + second;
            }else {
                strSecond = second + "";
            }
            total_tv.setText(strMinute + ":" + strSecond);
            Log.e(TAG,"handleMessage:" + strSecond);

            //歌曲现播放的时间
                String  pStrMinute = null;
                String pStrSecond = null;
                int pMinute = currentPosition / 1000 / 60;
                int pSecond = currentPosition /1000 % 60;
                if (pMinute <10){
                    pStrMinute = "0" +pMinute;

                }else{
                    pStrMinute = pMinute +"";
                }
                if(pSecond <10){
                    pStrSecond = "0" +pSecond;

                }else{
                    pStrSecond = pSecond +"";
                }
                progress_tv.setText(pStrMinute + ":" +pStrSecond);
        }
    };
//    @Override
//    public void onClick(View v) {
//        switch (v.getId()){
//            case R.id.play_btn:
//                musicControl.play();
//                break;
//            case R.id.pause_btn:
//                break;
//            case R.id.continue_btn:
//                break;
//            case R.id.exit_btn:
//                break;
//        }
//    }

    //创建消息处理器的对象 从子线程中获取传递过来的消息

    class MyServiceConn implements ServiceConnection{

        @Override
        public void onServiceConnected(ComponentName name, IBinder service) {
            musicControl = (MusicService.MusicControl) service;
        }

        @Override
        public void onServiceDisconnected(ComponentName name) {

        }


        }
    //解除服务
    private void unBind(Boolean isUnBind){
        if (! isUnBind){
            musicControl.pause();
            unbindService(conn);
            stopService(intent);
        }
    }
}